
//
//  SharePosterController.m
//  PosApp
//
//  Created by 刘萍 on 2018/10/11.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "SharePosterController.h"
#import "SharePoterCollectionCell.h"
#import "SharePosterModel.h"

@interface SharePosterController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UIImageView *ShowImageView;
@property (strong, nonatomic) IBOutlet UICollectionView *CollectionImageView;
@property (strong, nonatomic) NSMutableArray * cellArr;
@property (assign, nonatomic) NSUInteger ChooseNumb;
@property (strong, nonatomic) NSString * ChooseName;
@end

@implementation SharePosterController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"推广海报";
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(70, 60);
    layout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
    layout.minimumLineSpacing = 10;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.CollectionImageView.collectionViewLayout = layout;
    self.CollectionImageView.backgroundColor = [UIColor clearColor];
    self.CollectionImageView.delegate = self;
    self.CollectionImageView.dataSource = self;
    self.CollectionImageView.showsHorizontalScrollIndicator = NO;
    [self.CollectionImageView registerNib:[UINib nibWithNibName:@"SharePoterCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    [self UpLoadDataOfSharePosterController];
    
}


- (void)UpLoadDataOfSharePosterController{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:KUseriPhone forKey:@"phone"];
    
    [MainObjectWay PostWithMethod:@"index" andAction:@"Share" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"返回信息 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableArray * arr = responseObject[@"data"];
            for (int i = 0; i < arr.count; i ++) {
                NSMutableDictionary * dic = arr[i];
                SharePosterModel * model = [SharePosterModel mj_objectWithKeyValues:dic];
                [self.cellArr addObject:model];
            }
            
            if (self.cellArr.count > 0) {
                SharePosterModel * model = self.cellArr[0];
                NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
                [dic setObject:model.share_id forKey:@"share_id"];
                [dic setObject:KUseriPhone forKey:@"phone"];
                
                [MainObjectWay PostWithMethod:@"codeimg" andAction:@"Share" andAddParam:YES parameters:dic success:^(id responseObject) {
                    NSLog(@" responseObject = %@",responseObject);
                    if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
                        NSMutableDictionary * dic = responseObject[@"data"];
                        self.ChooseName = [NSString stringWithFormat:@"%@",dic[@"title"]];
                        [self.ShowImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",dic[@"url"]]]];
                    }
                } failure:^(NSError *error) {
                    NSLog(@"error = %@",error);
                }];
                
                [self.CollectionImageView reloadData];
            }
            
        }
    } failure:^(NSError *error) {
        NSLog(@"错误信息 = %@",error);
    }];
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.cellArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SharePoterCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.tag = indexPath.row+100;
    if (self.ChooseNumb == indexPath.row) {
        cell.ChooseImage.hidden = NO;
    }else{
        cell.ChooseImage.hidden = YES;
    }
    SharePosterModel * model = self.cellArr[indexPath.row];
    [cell setModel:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    SharePosterModel * model = self.cellArr[indexPath.row];
    
    self.ChooseNumb = indexPath.row;
    
    for (int i = 0; i < self.cellArr.count; i ++) {
        SharePoterCollectionCell * cell = (SharePoterCollectionCell *)[self.CollectionImageView viewWithTag:i+100];
        if (indexPath.row+100 == i+100) {
            cell.ChooseImage.hidden = NO;
            
        }else{
            NSLog(@"0000");
            cell.ChooseImage.hidden = YES;
            
        }
    }
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:model.share_id forKey:@"share_id"];
    [dic setObject:KUseriPhone forKey:@"phone"];
    
    [MainObjectWay PostWithMethod:@"codeimg" andAction:@"Share" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@" responseObject = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableDictionary * dic = responseObject[@"data"];
            self.ChooseName = [NSString stringWithFormat:@"%@",dic[@"title"]];
            [self.ShowImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",dic[@"url"]]]];
        }
    } failure:^(NSError *error) {
        NSLog(@"error = %@",error);
    }];
}

- (IBAction)ClickOnBtnOfSharePosterControllerWith01:(id)sender {
    NSData *data = UIImagePNGRepresentation(self.ShowImageView.image);
    // 多媒体消息中包含的图片数据对象
    WXImageObject *imageObject = [WXImageObject object];
    imageObject.imageData = data;
    
    WXMediaMessage *message = [WXMediaMessage message];
    // 设置消息缩略图的方法
    [message setThumbImage:[UIImage imageNamed:@"MainLogo"]];
    message.mediaObject = imageObject;
    
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = WXSceneSession;
    [WXApi sendReq:req];
    
}
- (IBAction)ClickOnBtnOfSharePosterControllerWith02:(id)sender {
    
    NSData *data = UIImagePNGRepresentation(self.ShowImageView.image);
    // 多媒体消息中包含的图片数据对象
    WXImageObject *imageObject = [WXImageObject object];
    imageObject.imageData = data;
    
    WXMediaMessage *message = [WXMediaMessage message];
    // 设置消息缩略图的方法
    [message setThumbImage:[UIImage imageNamed:@"MainLogo"]];
    message.mediaObject = imageObject;
    
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = WXSceneTimeline;
    [WXApi sendReq:req];
    
    
}
- (IBAction)ClickOnBtnOfSharePosterControllerWith03:(id)sender {
    
     UIImageWriteToSavedPhotosAlbum(self.ShowImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    
    NSString *msg = nil ;
    if(error != NULL){
        msg = @"保存图片失败" ;
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel  =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        }];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:^{
        }];
        
    }else{
        [MBProgressHUD showAlertViewWithText:@"保存成功"];
    }
}

- (void)SharePosterForWeiXinWithType:(int)type{
    
    
}

- (NSMutableArray *)cellArr {
    if (!_cellArr) {
        _cellArr = [NSMutableArray array];
    }
    return _cellArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
